package com.financial.service.platbalancepay.service.impl;

import com.financial.service.platbalancepay.config.UrlConstants;
import com.financial.service.platbalancepay.dao.AccountDao;
import com.financial.service.platbalancepay.dao.GybalanceWechatpayReceiptDao;
import com.financial.service.platbalancepay.domain.AccountDO;
import com.financial.service.platbalancepay.domain.GybalanceWechatpayReceiptDO;
import com.financial.service.platbalancepay.service.OperBalanceService;
import com.financial.service.platbalancepay.utils.balanceutils.AccessTokenUtil;
import com.financial.service.platbalancepay.utils.balanceutils.HttpClientUtils;
import com.financial.service.platbalancepay.utils.balanceutils.JsonUtils;
import com.financial.service.platbalancepay.utils.balanceutils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auter miaozhenfei
 * @date 2019/7/30 17:59
 */
@Service
public class OperBalanceServiceImpl implements OperBalanceService {

    public static final Logger LOGGER = LoggerFactory.getLogger(OperBalanceServiceImpl.class);

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private GybalanceWechatpayReceiptDao gybalanceWechatpayReceiptDao;

    public AccountDO getByCustomerUuid(String uuid) {
        Map<String, Object> map = new HashMap();
        map.put("customerUuid", uuid);
        map.put("delflag", "1");
        List<AccountDO> list = accountDao.list(map);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }


    public Double queryBalance(String customerUuid) {
        Double accountBalance = null;

        AccountDO account = this.getByCustomerUuid(customerUuid);
        if (null != account) {
            accountBalance = account.getAccountBalance();
        }
        return accountBalance;
    }


    @Override
    public boolean subFrozenOrUnfreezeBalance(Map map) {
        return false;
    }

    //将余额预支付信息发送转发够样优点平台 同步
    @Override
    public Map<String, Object> gyydbalancePayMQ(Map<String, Object> map) {
        LOGGER.info("gyydbalancePayMQ--------余额预支付信息发送转发够样优点平台 同步"+ JsonUtils.objectToJson(map));
        String result = "";
        try {
            result = httpClientUtils.doPost(map.get("notifyUrl").toString(), map);
            LOGGER.info("++++订单号orderNum:%s,余额预支付信息结果:%s", map.get("orderNum"), result);
            Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("++++订单号orderNum:%s,余额预支付失败:%s", map.get("orderNum"), e.getMessage());
        }
        return null;
    }

    public Map<String, Object> balancePayMQ(Map map) {
        LOGGER.info("balancePayMQ -------------- 请求同步"+JsonUtils.objectToJson(map));
        Map<String, Object> chageMap = null;
        String result = "";
        try {
            result = httpClientUtils.doPost(UrlConstants.BALANCE_MAKEPAY_URL, map);
            System.out.println("fanhui"+result);
            chageMap = JsonUtils.jsonToMap(result);
            LOGGER.info("++++customerUuid:%s,余额预支付信息结果:%s", map.get("customerUuid"), JsonUtils.objectToJson(chageMap));
            //todo 保存够样余额预支付同步回执
            String return_code = chageMap.get("return_code").toString();
            String message = chageMap.get("message").toString();
            String prePayInfoDO = chageMap.get("prePayInfoDO").toString();
            Map<String, Object> prePayInfoMap = JsonUtils.jsonToMap(prePayInfoDO);
            String customerUuid = prePayInfoMap.get("customerUuid").toString();
            String opeTime = prePayInfoMap.get("opeTime").toString();
            String payMoney = prePayInfoMap.get("payMoney").toString();
            String prePayNo = prePayInfoMap.get("prePayNo").toString();
            String state = prePayInfoMap.get("state").toString();
            String orderNum = prePayInfoMap.get("orderNum").toString();
            //保存回执信息
            GybalanceWechatpayReceiptDO balanceReceiptDO = new GybalanceWechatpayReceiptDO();
            balanceReceiptDO.setCode(return_code);
            balanceReceiptDO.setCustomeruuid(customerUuid);
            balanceReceiptDO.setGmtCreate(new Date());
            balanceReceiptDO.setGmtModified(new Date());
            balanceReceiptDO.setIsDelete("1");
            balanceReceiptDO.setMsg(message);
            balanceReceiptDO.setOpetime(opeTime);
            balanceReceiptDO.setOrderno(orderNum);
            balanceReceiptDO.setPlatfromappid(map.get("platfromId").toString());
            balanceReceiptDO.setPaymoney(new BigDecimal(payMoney));
            balanceReceiptDO.setPrepayno(prePayNo);
            balanceReceiptDO.setState(state);
            balanceReceiptDO.setResponseType("1");
            balanceReceiptDO.setSubject(map.get("subject").toString());
            gybalanceWechatpayReceiptDao.save(balanceReceiptDO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("++++customerUuid:%s,余额预支付失败:%s", map.get("customerUuid"), e.getMessage());
        }

        return chageMap;
    }

}
